//
//  WSDataReportFullScreenViewController.m
//  microvision
//
//  Created by zhenjingxiong on 2021/3/6.
//  Copyright © 2021 tencent. All rights reserved.
//

#import "WSDataReportFullScreenViewController.h"
#import "WSDataReportFullScreenCell.h"
#import "WSDataReportConfig.h"
#import "WSDataReportFilterManager.h"
#import "WSDataReportDetailViewController.h"
#import "WSDataReportHelper.h"
#import "WSDataReportDataSourceManager.h"
#import "WSShowFilterConditionView.h"
#import "WSDataReportToastView.h"

@interface WSDataReportFullScreenViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
UISearchBarDelegate,
WSDataReportManagerDelegate,
WSDataReportFullScreenDelegate,
WSDataReportDetailGoBackDelegate
>

@property (nonatomic, strong) UITableView *fullScreenViewlistView;
@property (nonatomic, strong) UIView *fullScreenHeadView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UIButton *updateFilterConditionButton;
@property (nonatomic, strong) UIButton *clearFilterConditionButton;
@property (nonatomic, strong) WSShowFilterConditionView *showFilterConditionView;

@end

@implementation WSDataReportFullScreenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [[WSDataReportFilterManager instance] registerObserver:self];
}

#pragma mark - setupUI Method

- (void)setupUI {
    self.title = @"数据上报列表";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(closeCurrentPage)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清空当前数据"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(clearCurrentReportData)];
    
    
    [self.view addSubview:self.fullScreenViewlistView];
    self.fullScreenViewlistView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self fullScreenViewlistViewConstraints]];
    
    self.fullScreenViewlistView.tableHeaderView = self.fullScreenHeadView;
    [self.fullScreenViewlistView  registerClass:[WSDataReportFullScreenCell class]
                         forCellReuseIdentifier:NSStringFromClass([WSDataReportFullScreenCell class])];
    
    [self.fullScreenHeadView addSubview:self.searchBar];
    self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.fullScreenHeadView addConstraints:[self searchBarConstraints]];
    
    [self.fullScreenHeadView addSubview:self.updateFilterConditionButton];
    self.updateFilterConditionButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.fullScreenHeadView addConstraints:[self updateFilterConditionButtonConstraints]];
    
    [self.fullScreenHeadView addSubview:self.clearFilterConditionButton];
    self.clearFilterConditionButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.fullScreenHeadView addConstraints:[self clearFilterConditionButtonConstraints]];
    
    [self.fullScreenHeadView addSubview:self.showFilterConditionView];
    self.showFilterConditionView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.fullScreenHeadView addConstraints:[self showFilterConditionViewConstraints]];
    
    NSString *filterConditionViewContent = [[WSDataReportFilterManager instance] filterConditionViewContent];
    [self.showFilterConditionView updateFilterConditionViewContent:filterConditionViewContent];
}


#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSDataReportFullScreenCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSDataReportFullScreenCell class]) forIndexPath:indexPath];
    [cell setShowLabelData:[WSDataReportFilterManager instance].filterDataSource[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [WSDataReportFilterManager instance].filterDataSource.count;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    NSDictionary *info = [WSDataReportFilterManager instance].filterDataSource[indexPath.row];
    WSDataReportDetailViewController *detailViewController = [[WSDataReportDetailViewController alloc] init];
    detailViewController.reportDataDictionary = info;
    detailViewController.delegate = self;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

#pragma mark - UISearchBarDelegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.view endEditing:YES];
    // 开始过滤数据
    [[WSDataReportFilterManager instance] updateSearchKey:searchBar.text];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchBar.text.length == 0) {
        [self performSelector:@selector(hideKeyboardWithSearchBar:) withObject:searchBar afterDelay:0];
    }
}


#pragma mark - WSDataReportManagerDelegate

- (void)updatedFilterDataSource {
    [self.fullScreenViewlistView reloadData];
}

- (void)updateFilterConditionViewContent:(NSString *)filterContent {
    [self.showFilterConditionView updateFilterConditionViewContent:filterContent];
}

#pragma mark - WSDataReportDetailGoBackDelegate
- (void)goBackFromDetailViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - WSDataReportFullScreenDelegate

- (void)goBackPreviousPage {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)closeFullScreenPage {
    self.view.hidden = NO;
}


- (void)confirmFilterCondition {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Private Method

- (void)clearCurrentReportData {
    [[WSDataReportDataSourceManager instance] removeCurrentAllReportData];
    [WSDataReportToastView showWithStatus:@"清除数据成功"];
}

- (void)closeCurrentPage {
    [[WSDataReportFilterManager instance] unregisterObserver:self];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([self.delegete respondsToSelector:@selector(closeFullScreenPage)]) {
        [self.delegete closeFullScreenPage];
    }
}


- (void)clearFilterCondition {
    [[WSDataReportFilterManager instance] clearFilterFilterConditions];
    [WSDataReportToastView showWithStatus:@"清除筛选条件成功"];
}

- (void)hideKeyboardWithSearchBar:(UISearchBar *)searchBar {
    [self.view endEditing:YES];
    [[WSDataReportFilterManager instance] updateSearchKey:searchBar.text];
}

#pragma mark - Accessor Method

- (WSShowFilterConditionView *)showFilterConditionView {
    if (!_showFilterConditionView) {
        _showFilterConditionView = [WSShowFilterConditionView new];
    }
    return _showFilterConditionView;
}

- (UIView *)fullScreenHeadView {
    if (!_fullScreenHeadView) {
        _fullScreenHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 150)];
        _fullScreenHeadView.backgroundColor = [UIColor  redColor];
    }
    return _fullScreenHeadView;
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        _searchBar.placeholder = @"请输入搜索的关键词";
        _searchBar.delegate = self;
    }
    return _searchBar;
}

- (UITableView *)fullScreenViewlistView {
    if (!_fullScreenViewlistView) {
        _fullScreenViewlistView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _fullScreenViewlistView.delegate = self;
        _fullScreenViewlistView.dataSource = self;
        _fullScreenViewlistView.tableFooterView = [UIView new];
        NSArray *array = [WSDataReportConfig instance].listViewShowKeyArray;
        _fullScreenViewlistView.rowHeight = 20 * array.count + 5;
        _fullScreenViewlistView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _fullScreenViewlistView;
}

- (UIButton *)updateFilterConditionButton {
    if (!_updateFilterConditionButton) {
        _updateFilterConditionButton = [UIButton new];
        _updateFilterConditionButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_updateFilterConditionButton setTitle:@"更新筛选条件" forState:UIControlStateNormal];
        [_updateFilterConditionButton addTarget:self action:@selector(goToUpdateFilterConditionPage)
                               forControlEvents:UIControlEventTouchUpInside];
    }
    return _updateFilterConditionButton;
}

- (UIButton *)clearFilterConditionButton {
    if (!_clearFilterConditionButton) {
        _clearFilterConditionButton = [UIButton new];
        _clearFilterConditionButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_clearFilterConditionButton setTitle:@"清除筛选条件" forState:UIControlStateNormal];
        [_clearFilterConditionButton addTarget:self action:@selector(clearFilterCondition)
                              forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearFilterConditionButton;
}

- (void)goToUpdateFilterConditionPage {
    WSUpdateFilterConditionViewController *vc = [[WSUpdateFilterConditionViewController alloc] init];
    vc.delegete = self;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Automatic layout Method

- (NSArray<NSLayoutConstraint *> *)fullScreenViewlistViewConstraints {
    NSLayoutConstraint *fullScreenViewlistViewLeading = [WSDataReportHelper equallyRelatedConstraintWithView:self.fullScreenViewlistView toItem:self.view attribute:NSLayoutAttributeLeading];
    NSLayoutConstraint *fullScreenViewlistViewTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.fullScreenViewlistView toItem:self.view attribute:NSLayoutAttributeTrailing];
    NSLayoutConstraint *fullScreenViewlistViewBottom = [WSDataReportHelper equallyRelatedConstraintWithView:self.fullScreenViewlistView toItem:self.view attribute:NSLayoutAttributeBottom];
    NSLayoutConstraint *fullScreenViewlistViewTop = [WSDataReportHelper equallyRelatedConstraintWithView:self.fullScreenViewlistView toItem:self.view attribute:NSLayoutAttributeTop];
    NSArray *constraints = @[fullScreenViewlistViewLeading, fullScreenViewlistViewTrailing, fullScreenViewlistViewBottom, fullScreenViewlistViewTop];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)searchBarConstraints {
    NSLayoutConstraint *searchBarViewTop = [NSLayoutConstraint constraintWithItem:self.searchBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.fullScreenHeadView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
    NSLayoutConstraint *searchBarViewLeading = [NSLayoutConstraint constraintWithItem:self.searchBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.fullScreenHeadView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];
    NSLayoutConstraint *searchBarViewTrailing = [NSLayoutConstraint constraintWithItem:self.searchBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.fullScreenHeadView attribute:NSLayoutAttributeRight multiplier:1.0 constant:10];
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.searchBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:40];
    NSArray *constraints = @[searchBarViewTop, searchBarViewLeading, searchBarViewTrailing, heightConstraint];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)updateFilterConditionButtonConstraints {
    NSLayoutConstraint *updateFilterConditionButtonTop = [NSLayoutConstraint constraintWithItem:self.updateFilterConditionButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    NSLayoutConstraint *updateFilterConditionButtonTrailing = [NSLayoutConstraint constraintWithItem:self.updateFilterConditionButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.fullScreenHeadView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
    NSArray *constraints = @[updateFilterConditionButtonTop, updateFilterConditionButtonTrailing];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)clearFilterConditionButtonConstraints {
    NSLayoutConstraint *clearFilterConditionButtonTop = [NSLayoutConstraint constraintWithItem:self.clearFilterConditionButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.updateFilterConditionButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    NSLayoutConstraint *clearFilterConditionButtonTrailing = [NSLayoutConstraint constraintWithItem:self.clearFilterConditionButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.updateFilterConditionButton attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    NSArray *constraints = @[clearFilterConditionButtonTop, clearFilterConditionButtonTrailing];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)showFilterConditionViewConstraints {
    NSLayoutConstraint *showFilterConditionViewLeading = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.fullScreenHeadView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];
    NSLayoutConstraint *showFilterConditionViewTop = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    NSLayoutConstraint *showFilterConditionViewRight = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.updateFilterConditionButton attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-10];
    NSLayoutConstraint *showFilterConditionViewBottom = [NSLayoutConstraint constraintWithItem:self.showFilterConditionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.fullScreenHeadView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10];
    NSArray *constraints = @[showFilterConditionViewLeading, showFilterConditionViewTop, showFilterConditionViewRight, showFilterConditionViewBottom];
    return constraints;
}



@end
